import { DESKTOP_PORT } from './src/env.ts';
import { defineConfig, devices } from '@playwright/test';
import path from 'node:path';

/**
 * Read environment variables from file.
 * https://github.com/motdotla/dotenv
 */
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });

const AMOUNT_OF_WORKERS = process.env.CI ? 1 : 4;

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
	testDir: './tests',
	/* Run tests in files in parallel */
	fullyParallel: true, // FullyParallel has some issues - for now we just parallelize by file.
	/* Fail the build on CI if you accidentally left test.only in the source code. */
	forbidOnly: !!process.env.CI,
	/* Retry on CI only */
	retries: process.env.CI ? 2 : 0,
	/* Opt out of parallel tests on CI. */
	workers: AMOUNT_OF_WORKERS,
	/* Reporter to use. See https://playwright.dev/docs/test-reporters */
	reporter: process.env.CI ? 'github' : 'list',
	/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
	use: {
		/* Base URL to use in actions like `await page.goto('/')`. */
		baseURL: `http://localhost:${DESKTOP_PORT}`,

		/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
		// I have no idea why, but with this disabled, some tests just always fail. I have no idea why.
		trace: 'on',
		video: process.env.CI ? 'on-first-retry' : 'retain-on-failure'
	},

	/* Configure projects for major browsers */
	projects: projects(),

	/* Run your local dev server before starting the tests */
	webServer: webServers()
});

function projects() {
	const projects = [];
	if (process.env.CI) {
		projects.push({
			name: 'chromium',
			use: { ...devices['Desktop Chrome'], viewport: { width: 1920, height: 1080 } }
		});
		return projects;
	}

	projects.push({
		name: 'Chrome',
		use: {
			...devices['Desktop Chrome'],
			headless: false,
			channel: 'chrome'
		}
	});

	return projects;
}

/**
 * Command to start the frontend server.
 * @param desktopPort - Optional port for the desktop application.
 */
function feServerCommand(desktopPort?: number) {
	const port = desktopPort ?? DESKTOP_PORT;
	return process.env.CI
		? `pnpm start:desktop --port ${port}`
		: `pnpm --filter @gitbutler/desktop dev --port ${port}`;
}

/**
 * Returns the Frontend server configuration for the playwright tests.
 *
 * If running locally, this returns a single server configuration what starts the dev server
 * on the default port.
 *
 * If running on CI, this returns multiple server configurations, one for each worker.
 * Each worker will start the dev server on a different port, so that they can run in parallel.
 */
function webServers() {
	const desktopPort = parseInt(DESKTOP_PORT, 10);
	const config = {
		cwd: path.resolve(import.meta.dirname, '../..'),
		command: feServerCommand(desktopPort),
		url: `http://localhost:${desktopPort}`,
		env: {
			// VITE_BUTLER_PORT: BUT_SERVER_PORT,
			VITE_BUTLER_HOST: 'localhost',
			VITE_BUILD_TARGET: 'web'
		},
		reuseExistingServer: true,
		stdout: 'pipe'
	} as const;

	return [config];
}
